package com.blockmeta.bbs.businesslibrary.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import com.blankj.utilcode.util.LogUtils;

import android.util.Log;
import android.view.Window;

import com.blankj.utilcode.util.LogUtils;
import com.blockmeta.bbs.baselibrary.base.app.BaseApp;
import com.blockmeta.bbs.businesslibrary.BaseBussinessApp;
import com.blockmeta.bbs.businesslibrary.R;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.pojo.PushParamsPOJO;
import com.blockmeta.bbs.businesslibrary.util.DeepLinkUtils;
import com.blockmeta.bbs.businesslibrary.widget.ForegroundCallbacks;
import com.google.gson.Gson;

import butterknife.ButterKnife;


public class DeepLinkActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseBussinessApp.getApp().setAppStatus(BaseBussinessApp.STATUS_NORMAL);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        if (intent.getExtras() != null && intent.getExtras().containsKey("parm7")) {
            boolean isForeground = ForegroundCallbacks.get(BaseApp.getApp()).isForeground();
            if (isForeground) {
                PushParamsPOJO pushParamsPOJO = new Gson().fromJson(intent.getExtras().getString("parm7"), PushParamsPOJO.class);
                if (!TextUtils.isEmpty(pushParamsPOJO.deeplink)) {
                    Uri uri=Uri.parse(pushParamsPOJO.deeplink);
                    DeepLinkUtils.jumpToTarget(this, uri);
                }
            } else {
                Intent launchIntent = this.getPackageManager().getLaunchIntentForPackage(this.getPackageName());
                launchIntent.putExtra(Constant.PUSH_JSON, intent.getExtras().getString("parm7"));
                this.startActivity(launchIntent);
            }
            finish();
        } else {
            Uri uri = intent.getData();
            if (uri==null){
                finish();
                return;
            }
            DeepLinkUtils.jumpToTarget(this, uri);
            finish();
        }
    }
}
